package com.gxa.learning.pc.controller;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Cost;
import com.gxa.learning.pc.service.FeedbackService;
import com.gxa.learning.pc.service.impl.CostServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/7 10:16
 */
@RestController
@RequestMapping("/Cost")
@Api(value = "成本添加,修改,删除")
public class CostController {

    @Autowired
    private CostServiceImpl costService;

    @Autowired
    private FeedbackService feedbackService;

    @PostMapping("/inset")
    @ApiOperation(value = "成本添加")
    public Map<String, Object> inset(@RequestBody Cost cost) throws Exception {
        costService.insertCost(cost);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除成本")
    @ApiImplicitParam(name = "map", value = "被删除的成本id,costId", paramType = "body", dataType = "map")
    public Map<String, Object> costDelete(@RequestBody Map<String, Object> map) throws Exception {
        System.out.println(map);
        Object costId = map.get("costId");
        if (costId == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        String s = costId.toString();
        costService.deleteCost(s);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("成本项修改,id必传")
    public Map<String, Object> update(@RequestBody Cost cost) throws Exception {
        System.out.println(cost);
        costService.updateCost(cost);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("修改成功");
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
